﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Nutilities.Net.Binary.Extensions
{
    public static class BinaryWriterExtension
    {
        public static void Write(this BinaryWriter writer, IWriteable writeable)
        {
            writeable.WriteTo(writer);
        }

        public static void Write<T>(this BinaryWriter writer, ICollection<T> collection)
            where T : IWriteable
        {
            writer.Write(collection.Count);
            foreach (T writeable in collection)
            {
                writer.Write(writeable);
            }
        }

        public static void Write<TKey, TValue>(this BinaryWriter writer, Dictionary<TKey, TValue> dictionary)
            where TKey : IWriteable
            where TValue : IWriteable
        {
            writer.Write(dictionary.Count);
            foreach (KeyValuePair<TKey, TValue> kvp in dictionary)
            {
                writer.Write(kvp.Key);
                writer.Write(kvp.Value);
            }
        }

        public static void Write(this BinaryWriter writer, DateTime dateTime)
        {
            writer.Write(dateTime.ToBinary());
        }

        public static void Write(this BinaryWriter writer, TimeSpan timeSpan)
        {
            writer.Write(timeSpan.Ticks);
        }
    }
}
